﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.MODEL;

namespace Project.DAL.System
{
    public class EmployeeDal
    {
        /// <summary>
        /// 上下文
        /// </summary>
        PSSEntities entities = DbContextFactory.Create();

        /// <summary>
        /// 获取员工数据
        /// </summary>
        /// <returns></returns>
        public DbSet<Employee> GetEmployee()
        {
            var datas = entities.Employee;
            return datas;
        }

        /// <summary>
        /// 添加员工
        /// </summary>
        /// <param name="emp"></param>
        /// <returns>添加受影响的行</returns>
        public int AddEmployee(Employee emp) {
            var datas = entities.Employee;
            datas.Add(emp);
            return entities.SaveChanges();
        }

        /// <summary>
        /// 修改员工
        /// </summary>
        /// <param name="emp"></param>
        /// <returns>修改受影响的行</returns>
        public int UpdateEmployee(Employee emp)
        {
            var datas = entities.Employee;
            var data = datas.Where(d => d.emID == emp.emID).FirstOrDefault();
            if (data ==null)
            {
                return -1; //没有找到修改的对象
            }
            else
            {
                //不能修改的项 就不用赋值
                data.emName = emp.emName;
                data.emSex = emp.emSex;
                data.phone = emp.phone;
                data.roleID = emp.roleID;
                data.cmdEmID = emp.cmdEmID;
                data.lastTime = emp.lastTime;
                data.status = emp.status;

                return entities.SaveChanges();
            }

           
        }

        /// <summary>
        /// 批量删除员工
        /// </summary>
        /// <param name="list"></param>
        /// <returns>删除受影响的行</returns>
        public int DeleteAll(List<Employee> list)
        {
            var datas = entities.Employee;
            datas.RemoveRange(list);
            return entities.SaveChanges();
        }
    }
}
